var app = getApp();
Page({
  data: {
    img1: "../../images/function/add.png",
    img2: "../../images/function/blank.png",
    img3: "../../images/function/blank.png",
    img4: "../../images/function/blank.png",
    img5: "../../images/function/blank.png",
    img6: "../../images/function/blank.png",
    imgCount: 0,
    imgInfo: {
      img1: { name: "", path: "" },
      img2: { name: "", path: "" },
      img3: { name: "", path: "" },
      img4: { name: "", path: "" },
      img5: { name: "", path: "" },
      img6: { name: "", path: "" },
    },
    callBackID: [],
    content: "",
    showPosition: false,
    position: "",
    type: '日常动态',
    typelist:['日常动态', '表白墙', '失物招领', '学术交流', '校园淘宝', '美食美景']
  }, reset() {
    wx.showToast({ title: '已发布' })
    this.setData({
      img1: "../../images/function/add.png",
      img2: "../../images/function/blank.png",
      img3: "../../images/function/blank.png",
      img4: "../../images/function/blank.png",
      img5: "../../images/function/blank.png",
      img6: "../../images/function/blank.png",
      imgCount: 0,
      imgInfo: {
        img1: { name: "", path: "" },
        img2: { name: "", path: "" },
        img3: { name: "", path: "" },
        img4: { name: "", path: "" },
        img5: { name: "", path: "" },
        img6: { name: "", path: "" },
      },
      callBackID: [],
      content: "",
      showPosition: false,
      position: "",
      type: '日常动态'
    })
    wx.navigateBack({
      delta: 0
    })
  }, onLoad() {
    wx.setNavigationBarTitle({ title:"发布" })
    if (!(app.globalData.hasUserInfo && app.globalData.database != null)) {
      wx.showToast({ title: '请登录并完善个人信息', icon: 'none', duration: 2000, success: function () { setTimeout(function () { wx.reLaunch({ url: '/pages/me/me', }) }, 2000) } })
    }
    var that = this
    wx.getLocation({
      type: 'wgs84',
      success(res) {
        wx.cloud.callFunction({ name: "function", data: { type: "getPosition", position: res.latitude + "," + res.longitude } }).then(res => {
          that.setData({ position: res.result.user })
        })
      }
    })
  }, clickImg(a) {
    var that = this
    var i = this.data.imgCount
    if (a.currentTarget.dataset.name == "img1") {
      if (that.data.imgCount == 0) {
        wx.chooseImage({
          count: 1, type: 'image', success(res) {
            var files = res.tempFiles[0]
            if (files != null) {
              that.data.imgInfo.img1.name = files.name
              that.data.imgInfo.img1.path = files.path
              that.setData({
                imgCount: i + 1,
                img1: files.path,
                img2: "../../images/function/add.png"
              })
            }
          }
        })
      } else if (this.data.imgCount >= 1) {
        wx.showModal({
          cancelText: '取消', confirmText: '确定', content: '是否删除该图片', showCancel: true, title: '提示', complete: (res) => {
            if (res.confirm) {
              var img2 = this.data.img2
              var img3 = this.data.img3
              var img4 = this.data.img4
              var img5 = this.data.img5
              var img6 = this.data.img6
              var info = this.data.imgInfo
              this.setData({
                img1: img2,
                img2: img3,
                img3: img4,
                img4: img5,
                img5: img6,
                img6: "../../images/function/blank.png",
                imgCount: i - 1
              })
              this.data.imgInfo.img1 = info.img2
              this.data.imgInfo.img2 = info.img3
              this.data.imgInfo.img3 = info.img4
              this.data.imgInfo.img4 = info.img5
              this.data.imgInfo.img5 = info.img6
              this.data.imgInfo.img6 = { name: "", path: "" }

            }
          },
        })
      }
    }
    if (a.currentTarget.dataset.name == "img2") {
      if (this.data.imgCount == 1) {
        wx.chooseImage({
          count: 1, type: 'image', success(res) {
            var files = res.tempFiles[0]
            if (files != null) {
              that.data.imgInfo.img2.name = files.name
              that.data.imgInfo.img2.path = files.path
              that.setData({
                imgCount: i + 1,
                img2: files.path,
                img3: "../../images/function/add.png"
              })
            }
          }
        })
      } else if (this.data.imgCount >= 2) {
        wx.showModal({
          cancelText: '取消', confirmText: '确定', content: '是否删除该图片', showCancel: true, title: '提示', complete: (res) => {
            if (res.confirm) {
              var img3 = this.data.img3
              var img4 = this.data.img4
              var img5 = this.data.img5
              var img6 = this.data.img6
              var info = this.data.imgInfo
              this.setData({
                img2: img3,
                img3: img4,
                img4: img5,
                img5: img6,
                img6: "../../images/function/blank.png",
                imgCount: i - 1
              })
              this.data.imgInfo.img2 = info.img3
              this.data.imgInfo.img3 = info.img4
              this.data.imgInfo.img4 = info.img5
              this.data.imgInfo.img5 = info.img6
              this.data.imgInfo.img6 = { name: "", path: "" }
            }
          },
        })
      }
    }
    if (a.currentTarget.dataset.name == "img3") {
      if (this.data.imgCount == 2) {
        wx.chooseImage({
          count: 1, type: 'image', success(res) {
            var files = res.tempFiles[0]
            if (files != null) {
              that.data.imgInfo.img3.name = files.name
              that.data.imgInfo.img3.path = files.path
              that.setData({
                imgCount: i + 1,
                img3: files.path,
                img4: "../../images/function/add.png"
              })
            }
          }
        })
      } else if (this.data.imgCount >= 3) {
        wx.showModal({
          cancelText: '取消', confirmText: '确定', content: '是否删除该图片', showCancel: true, title: '提示', complete: (res) => {
            if (res.confirm) {
              var img4 = this.data.img4
              var img5 = this.data.img5
              var img6 = this.data.img6
              var info = this.data.imgInfo
              this.setData({
                img3: img4,
                img4: img5,
                img5: img6,
                img6: "../../images/function/blank.png",
                imgCount: i - 1
              })
              this.data.imgInfo.img3 = info.img4
              this.data.imgInfo.img4 = info.img5
              this.data.imgInfo.img5 = info.img6
              this.data.imgInfo.img6 = { name: "", path: "" }
            }
          },
        })
      }
    }
    if (a.currentTarget.dataset.name == "img4") {
      if (this.data.imgCount == 3) {
        wx.chooseImage({
          count: 1, type: 'image', success(res) {
            var files = res.tempFiles[0]
            if (files != null) {
              that.data.imgInfo.img4.name = files.name
              that.data.imgInfo.img4.path = files.path
              that.setData({
                imgCount: i + 1,
                img4: files.path,
                img5: "../../images/function/add.png"
              })
            }
          }
        })
      } else if (this.data.imgCount >= 4) {
        wx.showModal({
          cancelText: '取消', confirmText: '确定', content: '是否删除该图片', showCancel: true, title: '提示', complete: (res) => {
            if (res.confirm) {
              var img5 = this.data.img5
              var img6 = this.data.img6
              var info = this.data.imgInfo
              this.setData({
                img4: img5,
                img5: img6,
                img6: "../../images/function/blank.png",
                imgCount: i - 1
              })
              this.data.imgInfo.img4 = info.img5
              this.data.imgInfo.img5 = info.img6
              this.data.imgInfo.img6 = { name: "", path: "" }
            }
          },
        })
      }
    }
    if (a.currentTarget.dataset.name == "img5") {
      if (this.data.imgCount == 4) {
        wx.chooseImage({
          count: 1, type: 'image', success(res) {
            var files = res.tempFiles[0]
            if (files != null) {
              that.data.imgInfo.img5.name = files.name
              that.data.imgInfo.img5.path = files.path
              that.setData({
                imgCount: i + 1,
                img5: files.path,
                img6: "../../images/function/add.png"
              })
            }
          }
        })
      } else if (this.data.imgCount >= 5) {
        wx.showModal({
          cancelText: '取消', confirmText: '确定', content: '是否删除该图片', showCancel: true, title: '提示', complete: (res) => {
            if (res.confirm) {
              var img6 = this.data.img6
              var info = this.data.imgInfo
              this.setData({
                img5: img6,
                img6: "../../images/function/blank.png",
                imgCount: i - 1
              })
              this.data.imgInfo.img5 = info.img6
              this.data.imgInfo.img6 = { name: "", path: "" }
            }
          },
        })
      }
    }
    if (a.currentTarget.dataset.name == "img6") {
      if (this.data.imgCount == 5) {
        wx.chooseImage({
          count: 1, type: 'image', success(res) {
            var files = res.tempFiles[0]
            if (files != null) {
              that.data.imgInfo.img6.name = files.name
              that.data.imgInfo.img6.path = files.path
              that.setData({
                imgCount: i + 1,
                img6: files.path,
              })
            }
          }
        })
      } else if (this.data.imgCount == 6) {
        wx.showModal({
          cancelText: '取消', confirmText: '确定', content: '是否删除该图片', showCancel: true, title: '提示', complete: (res) => {
            if (res.confirm) {
              this.setData({
                img6: "../../images/function/blank.png",
                imgCount: i - 1
              })
              this.data.imgInfo.img6 = { name: "", path: "" }
            }
          },
        })
      }
    }
  }, call(id) {
    this.data.callBackID.push(id)
    if (this.data.callBackID.length == this.data.imgCount) {
      var tem_position = ""
      if (this.data.showPosition) {
        tem_position = this.data.position
      } else {
        tem_position = ""
      }
      var time = Date.parse(new Date())
      var date = new Date(parseInt(time))
      var minutes=(date.getMinutes()<10)?"0"+date.getMinutes():date.getMinutes()
      var timeDes = date.getFullYear() + '年' + (date.getMonth() + 1) + "月" + date.getDate() + "日" + date.getHours() + ":" + minutes
      var d = {
        content: this.data.content,
        imgs: this.data.callBackID,
        userInfo: app.globalData.userInfo,
        postion: tem_position,
        updateTime: time / 1000,
        updateTimeDes:timeDes,
        type: this.data.type,
        user_id: app.globalData.database._id
      }
      wx.cloud.callFunction({ name: 'function', data: { type: "post", data: d } }).then(res => {
        this.reset()
      })
    }
  }, post() {
    if (this.data.imgCount == 0) {
      var tem_position = ""
      if (this.data.showPosition) {
        tem_position = this.data.position
      } else {
        tem_position = ""
      }
      var time = Date.parse(new Date())
      var date = new Date(parseInt(time))
      var minutes=(date.getMinutes()<10)?"0"+date.getMinutes():date.getMinutes()
      var timeDes = date.getFullYear() + '年' + (date.getMonth() + 1) + "月" + date.getDate() + "日" + date.getHours() + ":" + minutes
      var d = {
        content: this.data.content,
        imgs: [],
        userInfo: app.globalData.userInfo,
        postion: tem_position,
        updateTime: time / 1000,
        updateTimeDes: timeDes,
        type: this.data.type,
        user_id: app.globalData.database._id
      }
      wx.cloud.callFunction({ name: 'function', data: { type: "post", data: d } }).then(res => {
        this.reset()
      })
    } else {
      for (var i = 0; i < 6; i++) {
        var v = this.data.imgInfo["img" + (i + 1).toString()]
        if (v.path != "") {
          var stamp = Date.parse(new Date()).toString() + i.toString()
          wx.cloud.uploadFile({ cloudPath: stamp, filePath: v.path }).then(res => {
            this.call(res.fileID)
          })
        }
      }
    }
  }, bindInput(a) {
    this.setData({ content: a.detail.value })
  }, checkboxChange(a) {
    this.setData({ showPosition: (a.detail.value[0] == "1") })
  }, chooseType() {
    var that = this
    wx.showActionSheet({
      itemList: this.data.typelist,
      success: function (res) {
        that.setData({ type: that.data.typelist[res.tapIndex] })
      }
    })
  },bindPickerChange(e){
    this.setData({type:this.data.typelist[[e.detail.value]] })
  }
})